package com.example.demo;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Date;


public class Wpstest {

    public static void main(String[] args) {

        String AppId = "AK123";
        String AppKey = "sk456";
        String URL = "/api/v1/dosomething?name=xiaoming&age=18";
        String ContentType = "application/json";
        String Date = "Wed, 03 Nov 2021 02:55:55 GMT";
        String RequestBody = "";


//        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
//        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//        Date = sdf.format(new Date());
        System.out.println("格林威治时间为：" + Date);

        String ContentMd5 = Encryption.encrypByMD5(RequestBody);

        String Sha1=getSha1(AppKey + ContentMd5 + URL + ContentType + Date);
        String xAuth="WPS-3:"+AppId+":"+ Sha1;

        System.out.println("ContentType->"+ContentType);
        System.out.println("Date->"+Date);
        System.out.println("ContentMd5->"+ContentMd5);
        System.out.println("Sha1->"+Sha1);
        System.out.println("xAuth->"+xAuth);

    }

    public static String getSha1(String str) {

        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }


}
